package com.kyd.push.order.service.impl;

import com.kyd.push.common.utils.IdWorker;
import com.kyd.push.model.Order;
import com.kyd.push.model.OrderForm;
import com.kyd.push.model.OrderMqContext;
import com.kyd.push.model.PushLog;
import com.kyd.push.model.repository.PushLogRepository;
import com.kyd.push.mqservice.ProducerOrder;
import com.kyd.push.order.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private PushLogRepository pushLogRepository;

    @Override
    public void createOrder(OrderForm orderForm) {

        Order order = new Order();
        order.setId(IdWorker.getInstance().nextId()); // 订单ID生成
        BeanUtils.copyProperties(orderForm, order);


        //设置运单总价
        order.setTotalMoney(orderForm.getMoney());
        order.setPayMoney(orderForm.getMoney());


        PushLog pushLog = new PushLog();
        pushLog.setOrderId(order.getId());
        pushLog.setGmtCreate(new Date());
        PushLog log = pushLogRepository.save(pushLog);
        ProducerOrder.send(OrderMqContext.newInstance().set(order, log.getId()));
    }
}
